package com.example.testservicce.utils;

import android.media.AudioManager;
import android.media.MediaPlayer;

import java.io.IOException;

/**
 * Created by wly on 2019/5/21.
 */

public class MusicPlayUtils {
    public static MediaPlayer mp;
    public static void startPlay(String path){
        //
        if(mp!=null){

            if(mp.isPlaying()){
                mp.stop();
            }
            mp.release();

        }

        mp=new MediaPlayer();
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mp.setDataSource(path);
            mp.prepare();
            mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.start();
                }
            });
            mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    mp.release();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void stopPlay(){
        if (mp!=null&&mp.isPlaying()){
            mp.pause();
        }
    }
}
